package com.lzb.randomturntable.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.basic.utils.SPUtil;
import com.lzb.randomturntable.Content;
import com.lzb.randomturntable.R;
import com.lzb.randomturntable.databinding.DialogBombBinding;
import com.lzb.randomturntable.databinding.DialogBombSettingBinding;


public class BombSettingDialog extends BaseSmartDialog<DialogBombSettingBinding> implements View.OnClickListener {


    public BombSettingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_bomb_setting;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvConfirm.setOnClickListener(this);
        mDataBinding.ivCancel.setOnClickListener(this);

        int giftRow = SPUtil.getInt(getContext(), Content.giftRow, 5); // 默认5排
        int bombNumber = SPUtil.getInt(getContext(), Content.bombNumber, 3); // 默认3个炸弹

        mDataBinding.etGiftRow.setText(giftRow + "");
        mDataBinding.etBombNum.setText(bombNumber + "");
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            String bombNum = mDataBinding.etBombNum.getText().toString();
            if (TextUtils.isEmpty(bombNum)) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_bomb_num_tip));
                return;
            }
            String giftRow = mDataBinding.etGiftRow.getText().toString();
            if (TextUtils.isEmpty(giftRow)) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_gift_boxes_row_tip));
                return;
            }
            int bomb = Integer.parseInt(bombNum);
            int gift = Integer.parseInt(giftRow);

            if (bomb < 1 || bomb > 20) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_bomb_num_tip));
                return;
            }
            if (gift < 5 || gift > 9) {
                ToastUtils.showShort(getContext().getString(R.string.please_input_gift_boxes_row_tip));
                return;
            }

            SPUtil.putInt(getContext(), Content.bombNumber, bomb);
            SPUtil.putInt(getContext(), Content.giftRow, gift);
            dismiss();
            if (listener != null) {
                listener.clickConfirm();
            }
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }
}
